package com.lidroid.xutils.bitmap.core;

import ohos.app.Context;
import ohos.data.usage.DataUsage;
import ohos.media.image.PixelMap;
import ohos.data.usage.StatVfs;

import java.io.File;
import java.io.IOException;


public class BitmapCommonUtils {
    private BitmapCommonUtils() {

    }

    /**
     * 获取可以使用的缓存目录
     *
     * @param context
     * @param dirName 目录名称
     * @return
     */
    public static File getDiskCacheDir(Context context, String dirName) {
       /* final String cachePath = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ?
                getExternalCacheDir(context).getPath() : context.getCacheDir().getPath();*/

      /*  final String cachePath = context.getExternalCacheDir().exists() ?
                context.getExternalCacheDir().getAbsolutePath() : context.getCacheDir().getAbsolutePath();
        return new File(cachePath, dirName);
*/
        File result = null;

            File cacheDir = context.getExternalCacheDir();
            if (cacheDir != null) {
                result = new File(cacheDir, dirName);
            }
        if (result == null) {
            result = new File(context.getCacheDir(), dirName);
        }
        return result;
    }


    /**
     * 获取bitmap的字节大小
     *
     * @param bitmap
     * @return
     */
    public static int getBitmapSize(PixelMap bitmap) {
        //return bitmap.getBytesNumberPerRow() * bitmap.getImageInfo().size.height;
        return bitmap.getBytesNumberPerRow() * bitmap.getImageInfo().size.height;
    }


    /**
     * 获取程序外部的缓存目录
     *
     * @param context
     * @return
     */


    /**
     * 获取文件路径空间大小 stats.getBlockSize() ??
     *
     * @param path
     * @return
     */
    public static long getAvailableSpace(File path) {


        //     return (long) stats.getBlockSize() * (long) stats.getAvailableBlocks();

        if (!existsSdcard()) return 0;
        long blockSize = 0;
        long availableBlocks = 0;
        StatVfs stat = null;
        try {
            //path.getCanonicalPath()?
            stat = new StatVfs(path.getCanonicalPath());
            blockSize = stat.getSpace();
            availableBlocks = stat.getAvailableSpace();
        }
        catch (IOException e) {

        }
        return availableBlocks * blockSize;
    }

    public static Boolean existsSdcard() {
        //是否是真设备
        return DataUsage.isDiskEmulated();
    }

}
